<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .judgeWrapper {
            width: 600px;
            margin: 50px auto;
            user-select: none;
        }

        .judgeWrapper .judgeBox {
            position: relative;
        }

        .judgeWrapper .judgeBox img {
            width: 100%;
        }

        .judgeWrapper .judgeBox .boxA,
        .judgeWrapper .judgeBox .boxB {
            width: 50px;
            height: 50px;
            background-color: #fff;
            position: absolute;
        }

        .judgeWrapper .judgeBox .boxA {
            left: 0;
            top: 100px;
            background: url(../images/bg.png) no-repeat -350px -100px / 600px auto;
            /* background-size: 600px auto; */
            z-index: 9;
        }

        .judgeWrapper .judgeBox .boxB {
            left: 350px;
            top: 100px;
        }

        .judgeWrapper .slideBar {
            width: 100%;
            height: 50px;
            margin-top: 10px;
            border-radius: 25px;
            background-color: grey;
            position: relative;
        }

        .judgeWrapper .slideBar .ball {
            width: 50px;
            height: 50px;
            background-color: #6ff;
            border-radius: 50%;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <!-- .judge-wrapper>(.judgeBox>.boxA+.boxB)+.slideBar>.ball -->
    <div class="judgeWrapper">
        <div class="judgeBox">
            <img src="../images/bg.png" draggable="false" alt="">
            <div class="boxA"></div>
            <div class="boxB"></div>
        </div>
        <div class="slideBar">
            <div class="ball"></div>
        </div>
    </div>
</body>
<script>
    var slideBar = document.getElementsByClassName("slideBar")[0];
    var ball = document.getElementsByClassName("ball")[0];

    var boxA = document.getElementsByClassName("boxA")[0];
    var boxB = document.getElementsByClassName("boxB")[0];


    var maxLeft = slideBar.clientWidth - ball.clientWidth;

    ball.onmousedown = function (e) {
        var e = e || window.event;
        var posX = e.offsetX;
        var posY = e.offsetY;
        console.log(posX, posY);
        ball.style.transition = boxA.style.transition = "none";


        document.onmousemove = function (e) {
            var e = e || window.event;
            var x = e.pageX - slideBar.offsetLeft - posX;

            if (x < 0) x = 0;
            if (x > maxLeft) x = maxLeft;

            ball.style.left = x + "px";
            boxA.style.left = x + "px";
        }

        document.onmouseup = function () {
            document.onmousemove = null;

            // 松开时验证:  A盒子 和 B盒子 重叠?       =>  A盒子和B盒子的left值相同
            // 理想情况下  boxA.offsetLeft == boxB.offsetLeft  => 对手残党不友好

            // 设置误差允许范围
            // 1. boxA.offsetLeft >= boxB.offsetLeft -2 && boxA.offsetLeft <= boxB.offsetLeft + 2
            // 2. Math.abs( boxA.offsetLeft -  boxB.offsetLeft) <= 2


            if (Math.abs(boxA.offsetLeft - boxB.offsetLeft) <= 2) {
                console.log("success");
                location.href = "https://www.baidu.com";
            } else {
                console.log("fail");
                ball.style.left = 0;
                boxA.style.left = 0;

                // var a = b = 1;

                ball.style.transition = boxA.style.transition = "left 0.25s";
            }


        }

    }

</script>

</html>